// MainWindow.cs created with MonoDevelop
// User: juan at 22:05 12/02/2009
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using Gtk;

namespace monook{
	
	public partial class MainWindow: Gtk.Window
	{	

		private const string APP_NAME="Monook";
		
		private Gear gear;
		private OokFileFilter ookfilter;
		private NoFileFilter noFilter;
		private WorkTextView workTextView;
		private bool saved;
		private string fileName;
		
		public MainWindow (): base (Gtk.WindowType.Toplevel)
		{
			Build ();
			//iniciamos el editor de texto
			this.workTextView=new WorkTextView();
			this.workTextView.KeyPressEvent+=new KeyPressEventHandler(this.OnTextviewKeyPressEvent);
			this.vbox1.Add(workTextView);
			//controlador de ventana
			this.gear=new Gear();
			//creamos los filtros
			this.ookfilter=new OokFileFilter();
			this.noFilter=new NoFileFilter();
			//marcamos el archivo como no grabado
			this.saved=true;
			this.fileName=null;
			//encendemos la ventana
			this.SetTitle();
			this.ShowAll();
		}
		
		protected void OnDeleteEvent (object sender, DeleteEventArgs a)
		{
			Application.Quit ();
			a.RetVal = true;
		}

		protected virtual void OnTextviewKeyPressEvent (object o, Gtk.KeyPressEventArgs args)
		{
			string keyValue= args.Event.Key.ToString();
			this.saved=false;
			this.SetTitle();
		}

		protected virtual void OnQuitActionActivated (object sender, System.EventArgs e)
		{
			Application.Quit ();
		}

		protected virtual void OnAbrirActionActivated (object sender, System.EventArgs e)
		{
			FileChooserDialog chooser = new FileChooserDialog(
			                                       "Open File...",
			                                        this,
			                                        FileChooserAction.Open,
			                                       "Cancel",ResponseType.Cancel,
			                                       "Open",ResponseType.Accept);
			chooser.AddFilter(this.ookfilter);
			chooser.AddFilter(this.noFilter);
			if(chooser.Run() == (int) ResponseType.Accept){
				string fileContent=this.gear.Read(chooser.Filename);
				this.workTextView.Buffer.Text=fileContent;
				this.fileName=chooser.Filename;
				this.saved=true;
				this.SetTitle();
			}
			chooser.Destroy();
		}
		
		private bool IsNotSaved(){
			if(!this.saved){

			}
			return true; 
		}
		
		private void SetTitle(){
			this.Title=APP_NAME+" - ";
			if(this.fileName==null){
				this.Title+="[NOT SAVED]";
			}else
			{
				this.Title+=this.fileName;
			}
			if(!this.saved){
				this.Title+="*";			
			}
		}

		protected virtual void OnSaveAsActionActivated (object sender, System.EventArgs e)
		{
			FileChooserDialog chooser = new FileChooserDialog(
			                                       "Save as...",
			                                        this,
			                                        FileChooserAction.Save,
			                                       "Cancel",ResponseType.Cancel,
			                                       "Save",ResponseType.Accept);
			chooser.AddFilter(this.ookfilter);
			chooser.AddFilter(this.noFilter);
			if(chooser.Run() == (int) ResponseType.Accept){
				string fileContent=this.gear.Read(chooser.Filename);
				this.workTextView.Buffer.Text=fileContent;
				this.fileName=chooser.Filename;
				this.saved=true;
				this.SetTitle();
			}
			chooser.Destroy();
		}

	}

	class WorkTextView : TextView
	{
		protected override bool OnKeyPressEvent (Gdk.EventKey evnt)
		{
	        base.OnKeyPressEvent (evnt);
	        return false;
	    }
	}
}